package com.hxc.controller;

import com.hxc.pojo.User;
import com.hxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/check.html")
    public String skip(){
        return "/WEB-INF/jsp/login.jsp";
    }
    @RequestMapping("/doregister.html")
    public String doregister(){
        return "/WEB-INF/jsp/register.jsp";
    }

    @RequestMapping("/login.html")
    public String findUser(User user, Model model, HttpSession session){
        int i=userService.selectUser(user);
        if (i==0){
            model.addAttribute("message","用户名或密码错误");
            return "/WEB-INF/jsp/login.jsp";
        }
        session.setAttribute("SESSION_USER",user);
        return "/WEB-INF/jsp/successful.jsp";
    }

    @RequestMapping("/loginout.html")
    public String loginout(HttpSession session){
        session.invalidate();
        return "/WEB-INF/jsp/login.jsp";
    }

    @RequestMapping("/docheck.html")
    @ResponseBody
    public Map<String,Object> docheck(String userName){
        int count=userService.findByName(userName);
        Map<String,Object> result=new HashMap<>();
        if(count>0){
            result.put("status",false);
            result.put("msg","该用户名已被使用");
        }else {
            result.put("status",true);
            result.put("msg","用户名可以使用");
        }
        return result;
    }

}
